package com.tucy.logistics.modules.system.menu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tucy.logistics.common.dto.first.*;
import com.tucy.logistics.common.entity.user.SystemMenu;
import com.tucy.logistics.common.entity.user.SystemMenuMeta;
import com.tucy.logistics.modules.system.menu.service.SystemMenuService;
import com.tucy.logistics.modules.system.menu.mapper.SystemMenuMapper;
import com.tucy.logistics.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author tucy123
* @description 针对表【system_menu(菜单表)】的数据库操作Service实现
* @createDate 2023-09-07 15:29:44
*/
@Service
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> implements SystemMenuService{

    @Autowired
    RedisUtil redisUtil;

    /**
     * 查询菜单
     * @return
     */
    @Override
    public List<SystemMenu> queryAllMenus() {

        List<SystemMenu> menuList = this.list();
        menuList.forEach(systemMenu -> {
            SystemMenuMeta meta = new SystemMenuMeta();
            meta.setTitle(systemMenu.getName());
            meta.setIcon(systemMenu.getIcon());
            meta.setBreadcrumb(systemMenu.getBreadcrumb());
            systemMenu.setMeta(meta);
        });

        List<SystemMenu> result = menuList.stream().filter(menu -> menu.getParentId() == 0)
                .peek(parentMenu -> parentMenu.setChildren(getMenuChild(parentMenu, menuList)))
                .sorted(Comparator.comparing(SystemMenu::getSort))
                .collect(Collectors.toList());
        for (SystemMenu systemMenu : result) {
            if (ObjectUtil.isEmpty(systemMenu.getChildren())) {
                systemMenu.setChildren(null);
            }
        }
        return result;
    }

    private List<SystemMenu> getMenuChild(SystemMenu parentMenu, List<SystemMenu> menus) {
        return menus.stream().filter(childMenu -> childMenu.getParentId().equals(parentMenu.getId()))
                .peek(childMenu -> childMenu.setChildren(getMenuChild(childMenu,menus)))
                .sorted(Comparator.comparing(SystemMenu::getSort))
                .collect(Collectors.toList());
    }

    /**
     * 保存修改菜单
     * @param menus
     * @return
     */
    @Override
    public Boolean saveUpdateMenus(List<SystemMenu> menus) {
        List<SystemMenu> systemMenus = new LinkedList<>();

        for (SystemMenu menu : menus) {
            List<SystemMenu> children = menu.getChildren();
            if (ObjectUtil.isNotEmpty(children)) {
                for (SystemMenu child : children) {
                    SystemMenuMeta meta = child.getMeta();
                    child.setIcon(meta.getIcon());
                    child.setBreadcrumb(meta.getBreadcrumb());
                    child.setId(null);
                    systemMenus.add(child);
                }
            }
            SystemMenuMeta meta = menu.getMeta();
            menu.setIcon(meta.getIcon());
            menu.setBreadcrumb(meta.getBreadcrumb());
            menu.setId(null);
            systemMenus.add(menu);
        }

        return this.saveBatch(systemMenus);
    }

    /**
     * 首页数据
     * @return
     */
    @Override
    public WorkspaceDto workspace() {
        WorkspaceDto workspaceDto = new WorkspaceDto();

        OrganOverview organOverview = new OrganOverview();
        organOverview.setOrganName("西三旗分拣中心");
        organOverview.setOrganAddress("北京顺义区天澜综合保税区123号");
        organOverview.setOrganName("赵五金");
        organOverview.setPhone("16712345678");
        organOverview.setSortingCenterNumber(1);
        organOverview.setAgencyNumber(5);
        organOverview.setDriverNumber(48);
        organOverview.setCourierNumber(64);
        workspaceDto.setOrganOverview(organOverview);

        TodayData todayData = new TodayData();
        todayData.setOrderAmount(2284);
        todayData.setOrderAmountChanges(342);
        todayData.setOrderNumber(193);
        todayData.setOrderNumberChanges(27);
        todayData.setTransportTaskNumber(38);
        todayData.setTransportTaskNumberChanges(26);
        todayData.setTaskInTransitNumber(53);
        todayData.setTaskInTransitRate(42);
        todayData.setTaskInDeliveryNumber(34);
        todayData.setTaskInDeliveryRate(32);
        workspaceDto.setTodayData(todayData);

        Backlog backlog = new Backlog();
        backlog.setWaitingPickupNumber(39);
        backlog.setWaitingPickupRate(42);
        backlog.setWaitingDispatchNumber(25);
        backlog.setWaitingDispatchRate(56);
        backlog.setUnassignedTransportTaskNumber(85);
        backlog.setUnassignedTransportTaskRate(56);
        backlog.setTimeoutTransportTaskNumber(5);
        backlog.setTimeoutTransportTaskRate(4);
        workspaceDto.setBacklog(backlog);

        List<TransportTask> transportTaskList = new LinkedList<>();
        TransportTask transportTask = new TransportTask();
        TransportTask transportTask2 = new TransportTask();
        TransportTask transportTask3 = new TransportTask();
        TransportTask transportTask4 = new TransportTask();
        TransportTask transportTask5 = new TransportTask();

        transportTask.setId(15652478235201L);
        transportTask.setEndAgency("成都转运中心");
        transportTask.setStartAgency("杭州转运中心");
        transportTask.setLicensePlate("京A 891231");
        transportTask.setStatus(2);

        transportTask2.setId(15652478235201L);
        transportTask2.setEndAgency("天津转运中心");
        transportTask2.setStartAgency("杭州转运中心");
        transportTask2.setLicensePlate("京A 891231");
        transportTask2.setStatus(2);

        transportTask3.setId(15652478235201L);
        transportTask3.setEndAgency("天津转运中心");
        transportTask3.setStartAgency("广元转运中心");
        transportTask3.setLicensePlate("京B 891231");
        transportTask3.setStatus(2);

        transportTask4.setId(15652478235201L);
        transportTask4.setEndAgency("天津转运中心");
        transportTask4.setStartAgency("杭州转运中心");
        transportTask4.setLicensePlate("京A 891231");
        transportTask4.setStatus(2);

        transportTask5.setId(15652478235201L);
        transportTask5.setEndAgency("天津转运中心");
        transportTask5.setStartAgency("杭州转运中心");
        transportTask5.setLicensePlate("京A 891231");
        transportTask5.setStatus(2);

        transportTaskList.add(transportTask);
        transportTaskList.add(transportTask2);
        transportTaskList.add(transportTask3);
        transportTaskList.add(transportTask4);
        transportTaskList.add(transportTask5);
        workspaceDto.setTransportTaskList(transportTaskList);

        OrderLineChart orderLineChart = new OrderLineChart();
        orderLineChart.setOrderMaxNumber(54);
        orderLineChart.setOrderMinNumber(31);
        orderLineChart.setOrderAverageNumber(8);
        List<MonthlyOrder> monthlyOrderList = new LinkedList<>();
        MonthlyOrder monthlyOrder = new MonthlyOrder();
        monthlyOrder.setDateTime("2023-03");
        monthlyOrder.setOrderNumber(123);
        MonthlyOrder monthlyOrder2 = new MonthlyOrder();
        monthlyOrder2.setDateTime("2023-04");
        monthlyOrder2.setOrderNumber(124);
        monthlyOrderList.add(monthlyOrder);
        monthlyOrderList.add(monthlyOrder2);
        orderLineChart.setMonthlyOrderList(monthlyOrderList);
        workspaceDto.setOrderLineChart(orderLineChart);

        return workspaceDto;
    }
}




